package com.kfm.src.Demo0923;

import java.io.*;
import java.sql.*;

public class BookManager {

    private Statement ss;
    private Connection cc;


    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public BookManager()
    {
        init();
    }

    public void init() {
        String url = "jdbc:mysql://localhost:3306";
        String user = "root";
        String pass = "";
        try (Connection connection = DriverManager.getConnection(url, user, pass);){
            cc = connection;
            ss = connection.createStatement();
            ss.execute("show databases");
            ResultSet resultSet = ss.getResultSet();
            // 校验数据库是否存在
            int flag = 0;
            while (resultSet.next())
            {
                String database = resultSet.getString("database");
                if (strCompartto("manger", database))
                {
                    flag = 1;
                }
            }
            if (flag == 0)
            {
                ss.execute("create database manger");
            }
            flag = 0;
            ss.execute("use manger");
            ss.execute("show tables");
            ResultSet resultSet1 = ss.getResultSet();
            // 读取创建表的文件
            String createbooks;
            String cretelog;
            try ( FileInputStream fis = new FileInputStream("D:\\sql.txt");){
                byte[] bytes = fis.readAllBytes();
                String cre = new String(bytes);
                createbooks = cre.split(";")[0].trim() + ";";
                cretelog = cre.split(";")[1].trim() + ";";
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //System.out.println(createbooks);
            //System.out.println(cretelog);
            // 判断表是否存在
            ResultSet resultSet2 = ss.getResultSet();
            int flag1 = 0;
            while (resultSet2.next())
            {
                if (resultSet2.getString(1).equals("books"))
                {
                    flag = 1;
                }
                else if (resultSet2.getString(1).equals("operation_log"))
                {
                    flag1 = 1;
                }
            }
            if (flag == 0)
            {
                ss.execute(createbooks);
                if (flag1 == 0)
                {
                    ss.execute(cretelog);
                }
            }
            else if (flag1==0)
            {
                ss.execute(cretelog);
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /*
    增加图书
     */
    public void addBookInfo(books book)
    {

        String sqladd = "insert into books(id,book_title,publication_date,author,price,quantity) values(?,?,?,?,?,?)";
        String url = "jdbc:mysql://localhost:3306/manger";
        String user = "root";
        String pass = "";

        try (Connection connection = DriverManager.getConnection(url, user, pass);
             PreparedStatement p1 = connection.prepareStatement(sqladd)
        ){
            p1.setObject(1,book.getId());
            p1.setObject(2,book.getBook_tiltle());
            p1.setObject(3,book.getDate());
            p1.setObject(4,book.getAuthor());
            p1.setObject(5,book.getPrice());
            p1.setObject(6,book.getQuantity());
            p1.execute();
            System.out.println("添加成功");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    /*
    删除图书
     */

    public void removeBook()
    {

    }
    /*
    修改图书数量
     */
    public void modifyBookQuantity(){

    }
    /*
    显示所有图书
     */
    public void showAllBooks()
    {
        String sql = "SELECT * from books";
        String url = "jdbc:mysql://localhost:3306/manger";
        String user = "root";
        String pass = "";

        try (Connection connection = DriverManager.getConnection(url, user, pass);
             PreparedStatement p1 = connection.prepareStatement(sql)
        ){
            p1.execute();
            ResultSet resultSet = p1.getResultSet();
            while (resultSet.next())
            {
                System.out.println(resultSet.getInt(1) + "   " + resultSet.getString(2) + "   "+
                        resultSet.getString(3) + "   "+ resultSet.getString(4) + "   "
                        + resultSet.getString(5) + "   "+ resultSet.getString(6) + "   ");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    private boolean strCompartto(String s1,String s2)
    {
        if (s1.length() != s2.length())
        {
            return false;
        }
        for (int i= 0; i < s1.length(); i++) {
            if (s1.charAt(i) != s2.charAt(i))
            {
                return false;
            }
        }
        return true;
    }
}
